package com.example.practica1;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.StringWriter;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Activity7 extends Activity {
	int m;
	XmlPullParser parser;

	ArrayList lista = new ArrayList();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activity7);

		Bundle b = getIntent().getExtras();
		m = b.getInt("Menu");

		TextView tv = (TextView) findViewById(R.id.MostrarReglasGraficas);
		ImageView iv = (ImageView) findViewById(R.id.LlamarReglasGraficas);
		EditText et = (EditText) findViewById(R.id.LlamarReglas);
		EditText ti = (EditText) findViewById(R.id.LlamarInfo);
		EditText tscore = (EditText) findViewById(R.id.MostrarScore);

		System.out.println("OPCION DE MENU " + m);
		switch (m) {
		case 1:
			et.setVisibility(1);
			break;
		case 2:
			ti.setVisibility(1);
			break;
		case 3:
			tv.setVisibility(1);
			iv.setImageDrawable(getResources().getDrawable(R.drawable.juego));
			iv.setVisibility(1);

			break;
		case 4:

			// leerXML();

			// Añadir salto de linea al escribir

			FileInputStream fin = null;

			try {
				fin = openFileInput("ranking.xml");
			} catch (Exception e) {
				Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
			}

			parser = Xml.newPullParser();

			try {
				parser.setInput(fin, "UTF-8");

				int event = parser.next();
				while (event != XmlPullParser.END_DOCUMENT) {
					if (event == XmlPullParser.START_TAG) {
						Log.d("Fichero XML", "<" + parser.getName() + ">");
						for (int i = 0; i < parser.getAttributeCount(); i++) {
							Log.d("Fichero XML",
									"\t" + parser.getAttributeName(i) + " = "
											+ parser.getAttributeValue(i));

						}
					}

					if (event == XmlPullParser.TEXT
							&& parser.getText().trim().length() != 0) {
						Log.d("TEXTOS", "\t\t" + parser.getText());
						lista.add(parser.getText());
					}
					if (event == XmlPullParser.END_TAG)
						Log.d("END TAG", "</" + parser.getName() + ">");
					event = parser.next();
				}
				for (int i = 0; i < lista.size(); i++) {
					if(i%2==0){
						tscore.append("\n");
					}else tscore.append("  ");
					
					tscore.append(lista.get(i).toString());

					System.out.println(lista.get(i));
				}

				System.out.println(parser.toString());

				fin.close();

				Toast.makeText(this, "Leido correctamente", Toast.LENGTH_LONG)
						.show();
			} catch (Exception e) {
				Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
			}

			tscore.setVisibility(1);

			break;

		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity7, menu);
		return true;
	}

	@Override
	protected void onRestart() {
		finish();
		super.onRestart();
	}

	// private void leerXML() {
	// FileInputStream fin = null;
	//
	// try {
	// fin = openFileInput("ranking.xml");
	// } catch (Exception e) {
	// Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
	// }
	//
	// parser = Xml.newPullParser();
	//
	// try {
	// parser.setInput(fin, "UTF-8");
	//
	// int event = parser.next();
	// while (event != XmlPullParser.END_DOCUMENT) {
	// if (event == XmlPullParser.START_TAG) {
	// Log.d("Fichero XML", "<" + parser.getName() + ">");
	// for (int i = 0; i < parser.getAttributeCount(); i++) {
	// Log.d("Fichero XML", "\t" + parser.getAttributeName(i)
	// + " = " + parser.getAttributeValue(i));
	// }
	// }
	//
	// if (event == XmlPullParser.TEXT
	// && parser.getText().trim().length() != 0)
	// Log.d("Fichero XML", "\t\t" + parser.getText());
	//
	//
	// if (event == XmlPullParser.END_TAG)
	// Log.d("Fichero XML", "</" + parser.getName() + ">");
	//
	// event = parser.next();
	// }
	//
	// System.out.println(parser.toString());
	//
	// fin.close();
	//
	// Toast.makeText(this, "Leido correctamente", Toast.LENGTH_LONG)
	// .show();
	// } catch (Exception e) {
	// Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
	// }
	
	
	
}
